package com.tianya.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.dto.ExamRequestDTO;
import com.tianya.domain.entity.*;
import com.tianya.domain.vo.ExamVO;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.domain.vo.TeacherVO;
import com.tianya.service.*;
import com.tianya.mapper.ExamMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.awt.image.LookupOp;
import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【exam(考试表)】的数据库操作Service实现
* @createDate 2025-05-24 18:59:13
*/
@Service
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam>
    implements ExamService{
    @Resource
    private ClazzService clazzService;

    @Resource
    private CourseService courseService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private UserService userService;
    @Override
    public void createExam(ExamRequestDTO examRequestDTO) {
        Exam exam = new Exam();
        BeanUtil.copyProperties(examRequestDTO, exam);
        this.save(exam);
    }

    @Override
    public PageResultVO<ExamVO> pageExam(Integer pageNum, Integer pageSize, ExamRequestDTO examRequestDTO) {
        Page<Exam> page = Page.of(pageNum, pageSize);

        QueryWrapper<Exam> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(examRequestDTO.getName()!=null,  "name", examRequestDTO.getName());
        queryWrapper.like(examRequestDTO.getCourseId()!=null,  "course_id", examRequestDTO.getCourseId());
        queryWrapper.like(examRequestDTO.getClazzId()!=null,  "clazz_id", examRequestDTO.getClazzId());
        queryWrapper.like(examRequestDTO.getLocation()!=null,  "location", examRequestDTO.getLocation());

        Page<Exam> examPage = this.page(page, queryWrapper);
        PageResultVO<ExamVO> pageVO = new PageResultVO<>();
        List<ExamVO> examVOList = new ArrayList<>();
        for (Exam exam : examPage.getRecords()) {
            ExamVO examVO = new ExamVO();
            Long courseId = exam.getCourseId();
            Long clazzId = exam.getClazzId();
            Long teacherId = exam.getTeacherId();
            Long courseTeacherId = exam.getCourseTeacherId();

            Course course = courseService.getById(courseId);
            Clazz clazz = clazzService.getById(clazzId);
            Teacher teacher = teacherService.getById(teacherId);
            Teacher teacherTeacher = teacherService.getById(courseTeacherId);
            User userTeacher = userService.getById(teacherTeacher!=null?teacherTeacher.getUserId():null);
            User user = userService.getById(teacher!=null?teacher.getUserId():null);

            examVO.setCourseName(course!=null?course.getName():"unKnown");
            examVO.setClazzName(clazz!=null?clazz.getName():"unKnown");
            examVO.setTeacherName(user!=null?user.getName():"unKnown");
            examVO.setCourseTeacherName(userTeacher!=null?userTeacher.getName():"unKnown");


            BeanUtil.copyProperties(exam, examVO);
            examVOList.add(examVO);
        }
        pageVO.setTotal(examPage.getTotal());
        pageVO.setRecords(examVOList);
        return pageVO;
    }

    @Override
    public List<ExamVO> listExam() {
        List<Exam> examList = this.list();
        List<ExamVO> examVOList = new ArrayList<>();
        for (Exam exam : examList) {
            ExamVO examVO = new ExamVO();
            Long courseId = exam.getCourseId();
            Long clazzId = exam.getClazzId();
            Long teacherId = exam.getTeacherId();
            Long courseTeacherId = exam.getCourseTeacherId();

            Course course = courseService.getById(courseId);
            Clazz clazz = clazzService.getById(clazzId);
            Teacher teacher = teacherService.getById(teacherId);
            Teacher teacherTeacher = teacherService.getById(courseTeacherId);
            User userTeacher = userService.getById(teacherTeacher!=null?teacherTeacher.getUserId():null);
            User user = userService.getById(teacher!=null?teacher.getUserId():null);

            examVO.setCourseName(course!=null?course.getName():"unKnown");
            examVO.setClazzName(clazz!=null?clazz.getName():"unKnown");
            examVO.setTeacherName(user!=null?user.getName():"unKnown");
            examVO.setCourseTeacherName(userTeacher!=null?userTeacher.getName():"unKnown");


            BeanUtil.copyProperties(exam, examVO);
            examVOList.add(examVO);
        }
        return examVOList;
    }

    @Override
    public List<ExamVO> getExamByClazzId(Long clazzId) {
        List<Exam> examList = this.list(new QueryWrapper<Exam>()
                .eq("clazz_id", clazzId));
        List<ExamVO> examVOList = new ArrayList<>();
        for (Exam exam : examList) {
            ExamVO examVO = new ExamVO();
            Long courseId = exam.getCourseId();
            Long teacherId = exam.getTeacherId();
            Long courseTeacherId = exam.getCourseTeacherId();

            Course course = courseService.getById(courseId);
            Clazz clazz = clazzService.getById(clazzId);
            Teacher teacher = teacherService.getById(teacherId);
            Teacher teacherTeacher = teacherService.getById(courseTeacherId);
            User userTeacher = userService.getById(teacherTeacher!=null?teacherTeacher.getUserId():null);
            User user = userService.getById(teacher!=null?teacher.getUserId():null);

            examVO.setCourseName(course!=null?course.getName():"unKnown");
            examVO.setClazzName(clazz!=null?clazz.getName():"unKnown");
            examVO.setTeacherName(user!=null?user.getName():"unKnown");
            examVO.setCourseTeacherName(userTeacher!=null?userTeacher.getName():"unKnown");

            BeanUtil.copyProperties(exam, examVO);
            examVOList.add(examVO);
        }
        return examVOList;
    }

    @Override
    public ExamVO getDetail(Long id) {
        Exam exam = this.getById(id);
        ExamVO examVO = new ExamVO();
        Long courseId = exam.getCourseId();
        Long teacherId = exam.getTeacherId();
        Long courseTeacherId = exam.getCourseTeacherId();
        Long clazzId = exam.getClazzId();
        Course course = courseService.getById(courseId);
        Clazz clazz = clazzService.getById(clazzId);
        Teacher teacher = teacherService.getById(teacherId);
        Teacher teacherTeacher = teacherService.getById(courseTeacherId);
        User userTeacher = userService.getById(teacherTeacher!=null?teacherTeacher.getUserId():null);
        User user = userService.getById(teacher!=null?teacher.getUserId():null);

        examVO.setCourseName(course!=null?course.getName():"unKnown");
        examVO.setClazzName(clazz!=null?clazz.getName():"unKnown");
        examVO.setTeacherName(user!=null?user.getName():"unKnown");
        examVO.setCourseTeacherName(userTeacher!=null?userTeacher.getName():"unKnown");

        BeanUtil.copyProperties(exam, examVO);
        return examVO;
    }
}




